/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.networking;

import java.util.Vector;


/**
 * Operation for retrieving blog comments.
 */
public class GetCommentsOperation extends NetworkOperation {

    /**
     * Listener interface
     */
    public interface Listener {

        /**
         * Returns the retrieved comments or null in case of an error.
         * @param comments
         */
        public void commentsReceived(Vector comments);
    }
    private static final String WP_GET_COMMENTS = "wp.getComments";
    private Blog blog;
    private Vector posts;
    private Listener listener;

    /**
     * Cosntructor
     * @param listener
     * @param blog Blog to read comments from
     * @param posts Blog posts, references of relevant posts are attached to the comments for convenience.
     */
    public GetCommentsOperation(Listener listener, Blog blog, Vector posts) {
        this.listener = listener;
        this.blog = blog;
        this.posts = posts;
    }

    /**
     * Starts the operation.
     */
    public void start() {
        Request req = new Request(WP_GET_COMMENTS);
        req.addStringParam(blog.blogId);
        req.addStringParam(blog.blogUser);
        req.addStringParam(blog.blogUserPassword);
        //req.addStructMember("post_id", post.postId); // If this is not added, returns all comments.
        req.addStructMember("number", "50"); // get this many comments
        Network nw = new Network(this);
        nw.startHttpPost(blog.xmlrpcUrl, req.request());
    }

    /**
     * Parses the server response and calls the listener.
     * For convenience, a reference to the parent Post is attached to each Comment if possible.
     * @param response
     */
    public void networkHttpPostResponse(String response) {
        try {
            CommentsParser handler = new CommentsParser();
            parse(response, handler);

            // Attach Post references to each Comment 
            Vector comments = handler.comments;
            for (int c = 0; c < comments.size(); c++) {
                Comment comment = (Comment) comments.elementAt(c);
                comment.post = null;
                System.out.println("Got comment: " + comment.commentId + ", postId: " + comment.postId);
                for (int p = 0; p < posts.size(); p++) {
                    Post post = (Post) posts.elementAt(p);
                    if (post.postId.equals(comment.postId)) {
                        comment.post = post;
                        break;
                    }
                }
            }
            listener.commentsReceived(comments);

        } catch (ParseError e) {
            listener.commentsReceived(null);
        }
    }
}
